#!/usr/bin/python
# -*- coding: utf-8 -*-

#取出数据库表的字段和注释
import MySQLdb
import json
import sys
import urllib2

HOST = "127.0.0.1"
PORT = 4406
DB = "coacht"
def getTables():
    conn = MySQLdb.connect(host=HOST,user="for_ol",passwd="Abcd1234",port=PORT)
    cur = conn.cursor()
    conn.select_db(DB)
    sql = "show tables"
    cur.execute(sql)
    row = cur.fetchall()
    cur.close()
    conn.close()
    return row

def getTableColumns(tableName):
    conn = MySQLdb.connect(host=HOST,user="for_ol",passwd="Abcd1234",port=PORT,cursorclass = MySQLdb.cursors.DictCursor)
    cur = conn.cursor()
    conn.select_db(DB)
    sql = "show full columns from "+tableName
    cur.execute(sql)
    row = cur.fetchall()
    cur.close()
    conn.close()
    return row

if __name__ == "__main__":
    tables = getTables()
    for table in tables:
        tableName = table[0];
        columns = getTableColumns(tableName)
        print "表名:%s" %  tableName
        for field in columns:
            print "%-20s\t%s" % (field['Field'],field['Comment'])
        print "\n"
